package com.daixu.xutil.demo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.daixu.xutil.demo.BaseActivity;
import com.daixu.xutil.demo.R;
import com.daixu.xutil.demo.adapter.HeaderViewRecyclerAdapter;
import com.daixu.xutil.demo.adapter.RefreshAdapter;
import com.daixu.xutil.demo.utils.EndlessRecyclerOnScrollListener;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangbo on 2016/2/19.
 */
@ContentView(R.layout.activity_recycler_view)
public class RecyclerViewActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @ViewInject(R.id.swipeRefreshLayout)
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<String> mList = new ArrayList<>();
    @ViewInject(R.id.recycler_view)
    private RecyclerView recyclerView;
    private HeaderViewRecyclerAdapter stringAdapter;
    private String TAG = RecyclerViewActivity.class.getSimpleName();
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 顶部刷新的样式
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_green_light, android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        setData();
        RefreshAdapter refreshAdapter = new RefreshAdapter(mList, this);
        stringAdapter = new HeaderViewRecyclerAdapter(refreshAdapter);
        recyclerView.setAdapter(stringAdapter);
        createLoadMoreView();

        mHandler = new Handler();

        swipeRefreshLayout.setOnRefreshListener(this);
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        simulateLoadMoreData();
                    }
                }, 2000);
            }
        });
    }

    private void simulateLoadMoreData() {
        loadMoreData();
        stringAdapter.notifyDataSetChanged();
        Toast.makeText(this, "Load Finished!", Toast.LENGTH_SHORT).show();
    }

    private void loadMoreData() {
        List<String> moreList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            moreList.add("加载更多的数据");
        }
        mList.addAll(moreList);
    }

    private void setData() {
        mList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mList.add("第" + i + "个");
        }
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                fetchingNewData();
                swipeRefreshLayout.setRefreshing(false);
                stringAdapter.notifyDataSetChanged();
                Toast.makeText(RecyclerViewActivity.this, "Refresh Finished!", Toast.LENGTH_SHORT).show();
            }
        }, 2000);
    }

    private void fetchingNewData() {
        mList.add(0, "下拉刷新出来的数据");
    }

    private void createLoadMoreView() {
        View loadMoreView = LayoutInflater
                .from(this)
                .inflate(R.layout.view_load_more, recyclerView, false);
        stringAdapter.addFooterView(loadMoreView);
    }
}
